import java.util.concurrent.CountDownLatch;

public class demo3 {
    public static void main(String[] args) throws InterruptedException {
        //10个线程，await就会在10次调用完countDown之后才能继续执行
        CountDownLatch countDownLatch=new CountDownLatch(10);
        for (int i = 0; i < 10; i++) {//若只循环9次是不会执行await()的，也就无法完成任务
            int id=i;
            Thread t=new Thread(()->{
                System.out.println("Thread"+id);
                try {
                    Thread.sleep(300);//相当于其中一个线程要执行其任务的时间
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                countDownLatch.countDown();
            });
            t.start();
        }
        countDownLatch.await();
        System.out.println("所有任务都完成了！");
    }
}
